HomePromotion.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use client";
  2. import { PromotionRep } from "@/api/home";
  3. import Box from "@/components/Box";
  4. import { CenterPopup } from "antd-mobile";
  5. import { FC, useEffect, useState } from "react";
  6. import dayjs from "dayjs";
  7. import { Pagination } from "swiper/modules";
  8. import { Swiper, SwiperSlide } from "swiper/react";
  9. interface Props {
  10. data?: PromotionRep[];
  11. type?: 1 | 2; // 每天只弹一次 / 每次登录都弹
  12. }
  13. const HomePromotion: FC<Props> = (props) => {
  14. const { data, type } = props;
  15. const [visible, setVisible] = useState(false);
  16. useEffect(() => {
  17. const isClosePromotion = sessionStorage.getItem("isClosePromotion");
  18. // 如果 type 为 1 && 有isNow为true 表示已经弹出,
  19. const shouldShowPromotion = () => {
  20. // 如果不等于1而数据又是时间,清除
  21. if (type !== 1 && dayjs().isSame(isClosePromotion, "days")) {
  22. sessionStorage.removeItem("isClosePromotion");
  23. return true;
  24. }
  25. if (type === 1) {
  26. return !dayjs().isSame(isClosePromotion, "days");
  27. } else if (type === 2) {
  28. return !isClosePromotion;
  29. }
  30. return false;
  31. };
  32. let flag = shouldShowPromotion();
  33. setVisible(flag);
  34. }, []);
  35. const closeHandler = () => {
  36. setVisible(false);
  37. if (type === 1) {
  38. sessionStorage.setItem("isClosePromotion", dayjs().format("YYYY-MM-DD"));
  39. }
  40. if (type === 2) {
  41. sessionStorage.setItem("isClosePromotion", "true");
  42. }
  43. };
  44. if (data && data.length === 0) return null;
  45. return (
  46. <div>
  47. <CenterPopup visible={visible} onMaskClick={closeHandler}>
  48. <div className={"promotion-swiper relative max-w-[3.139rem]"}>
  49. <div
  50. onClick={closeHandler}
  51. className={
  52. "iconfont icon-guanbi absolute right-[0.1389rem] top-[0.0347rem]" +
  53. " z-20"
  54. }
  55. ></div>
  56. <Swiper
  57. loop
  58. pagination={{ clickable: true }}
  59. spaceBetween={10}
  60. scrollbar={{ draggable: true }}
  61. modules={[Pagination]}
  62. slidesPerView={1}
  63. grabCursor={true}
  64. navigation={true}
  65. className={"min-h-[2.8472rem] rounded-[0.0694rem] bg-[#1c1c1c]"}
  66. >
  67. {data?.map((promotion, index) => (
  68. <SwiperSlide key={index}>
  69. <div className={"h-[0.2778rem] px-[0.12rem]"}>
  70. <header className={"pb-[0.0694rem] leading-[0.2778rem]"}>
  71. {promotion.content.title}
  72. </header>
  73. </div>
  74. <Box
  75. action={promotion.action_type}
  76. actionData={promotion.action_params}
  77. >
  78. <img
  79. className={"h-[2.3rem] w-[100%] pb-[0.1111rem]"}
  80. src={promotion.content.image}
  81. alt={promotion.content.title}
  82. />
  83. </Box>
  84. </SwiperSlide>
  85. ))}
  86. </Swiper>
  87. </div>
  88. </CenterPopup>
  89. </div>
  90. );
  91. };
  92. export default HomePromotion;